﻿using System.Text.Json;
using System.Text.Json.Serialization;

namespace net8.Format
{
    /// <summary>
    /// GUID 格式化
    /// </summary>
    public class GuidFormat : JsonConverter<Guid>
    {
        public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            string value = reader.GetString() ?? "";
            if (string.IsNullOrEmpty(value))
            {
                value = "00000000-0000-0000-0000-000000000000";
            }

            return Guid.Parse(value);
        }

        public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString().ToUpper());
        }
    }
}
